﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;
using Bussines.Utils;
using Entities;

namespace Website.Administrator
{
    public partial class TinTucDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                DanhMucService danhMucService = new DanhMucService();
                Hashtable hashtable = new Hashtable();
                hashtable["pid"] = 3;
                List<DanhMuc> danhMucs = danhMucService.Search(hashtable, "id", "DESC", 0);
                ddlDanhMucCha.DataSource = danhMucs;
                ddlDanhMucCha.DataTextField = "ten";
                ddlDanhMucCha.DataValueField = "id";
                ddlDanhMucCha.DataBind();
                int pid = Int32.Parse(ddlDanhMucCha.SelectedValue.ToString());
                LoadDataDDLCon(pid);
                if (Request.QueryString["MaTinTuc"] != null)
                {
                    TinTucService tinTucService = new TinTucService();
                    TinTuc tinTuc = tinTucService.Select(Int32.Parse(Request.QueryString["MaTinTuc"]));
                    DanhMuc danhMuc = danhMucService.Select(tinTuc.Loaitin);
                    if (danhMuc.Pid != 51)
                    {
                        ddlDanhMucCha.SelectedValue = danhMuc.Pid.ToString();
                        ddlDanhMucCha.DataBind();
                        ddlDanhMucCon.SelectedValue = danhMuc.Id.ToString();
                    }
                    else
                    {
                        ddlDanhMucCha.SelectedValue = danhMuc.Id.ToString();
                        ddlDanhMucCha.DataBind();
                        ddlDanhMucCon.Visible = false;
                    }
                    txtMa.Value = tinTuc.Id.ToString();
                    txtBoxMoTa.Text = tinTuc.Mota;
                    txtTieuDe.Text = tinTuc.Tieude;
                    //CKEditorControl1.t = tinTuc.Noidung;
                    //imgUpLoad.ImageUrl = @"/" + tinTuc.Filehinh;
                    ibtThemMoi.Visible = false;
                }
                else
                {
                    ibtUpdate.Visible = false;
                }
            }
        }
        private void LoadDataDDLCon(int pid)
        {
            DanhMucService danhMucService = new DanhMucService();
            List<DanhMuc> danhMucs = new List<DanhMuc>();
            Hashtable hashtable = new Hashtable();
            hashtable["pid"] = pid;
            danhMucs = danhMucService.Search(hashtable, "id", "DESC", 0);
            ddlDanhMucCon.DataSource = danhMucs;
            ddlDanhMucCon.DataTextField = "ten";
            ddlDanhMucCon.DataValueField = "id";
            ddlDanhMucCon.DataBind();
            if (ddlDanhMucCon.Items.Count > 0)
            {
                ddlDanhMucCon.Visible = true;
            }
            else
            {
                ddlDanhMucCon.Visible = false;
            }
            if (pid == 19 || pid == 20  || pid == 33)
            {
                MultiView1.ActiveViewIndex=0;
                hashtable.Clear();
                hashtable["pid"] = 27;
                danhMucs = danhMucService.Search(hashtable, "id", "DESC", 0);
                ddlSanPham.DataSource = danhMucs;
                ddlSanPham.DataTextField = "ten";
                ddlSanPham.DataValueField = "id";
                ddlSanPham.DataBind();
            }
            else
            {
                MultiView1.ActiveViewIndex = -1;
            }

        }

        protected void ButtonUpload_Click(object sender, EventArgs e)
        {
           // String urlImg = Utils.FilePhoto_Upload(FileUpload1, "../upload/img/");
           // if (urlImg != "")
            {
          //      imgUpLoad.ImageUrl = @"../upload/img/" + urlImg;
            }

        }


        protected void ddlDanhMucCha_SelectedIndexChanged(object sender, EventArgs e)
        {

            int pid = Int32.Parse(ddlDanhMucCha.SelectedValue.ToString());
            LoadDataDDLCon(pid);
           
        }
        private void AddDataTinTuc(TinTuc tinTuc)
        {
            if (ddlDanhMucCon.Visible)
            {
                tinTuc.Loaitin = Int32.Parse(ddlDanhMucCon.SelectedValue.ToString());
            }
            else
            {
                tinTuc.Loaitin = Int32.Parse(ddlDanhMucCha.SelectedValue.ToString());
            }
            tinTuc.Mota = txtBoxMoTa.Text;
           // tinTuc.Noidung = CKEditorControl1.Text;
            String img = imgUpLoad.ImageUrl;
            if (img[1] == '.')
            {
                img = img.Substring(3);
            }
            if (img[0] == '/')
                img = img.Substring(1);
           // tinTuc.Filehinh = img;
            tinTuc.Tieude = txtTieuDe.Text;
            tinTuc.Ngaycapnhat = DateTime.Parse(DateTime.Now.ToShortDateString());
           
        }
        protected void ibtThemMoi_Click(object sender, ImageClickEventArgs e)
        {
            TinTucService tinTucService = new TinTucService();
            TinTuc tinTuc = new TinTuc();
            AddDataTinTuc(tinTuc);
            tinTucService.Insert(tinTuc);

        }

        protected void ibtUpdate_Click(object sender, ImageClickEventArgs e)
        {
            TinTucService tinTucService = new TinTucService();
            String rqMa = Request.QueryString["MaTinTuc"];
            TinTuc tinTuc = tinTucService.Select(Int32.Parse(rqMa));
            AddDataTinTuc(tinTuc);
            tinTucService.Update(tinTuc);
            MessageBox.Show("update Thanh Cong!");
            Response.Redirect("TinTuc.aspx");
        }

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            if (Request.QueryString["MaTinTuc"] != null)
            {
                Response.Redirect("TinTucThem.aspx?MaTinTuc=" + Request.QueryString["MaTinTuc"]);
            }
            else
            {
                Response.Redirect("TinTucThem.aspx");
            }
        }
    }
}